from sqlalchemy.orm import Session
from typing import Optional, List
from app.models.iaro_career.gateway_school import GatewaySchool
from app.schema.gateway_school_schema import GatewaySchoolCreate, GatewaySchoolUpdate

def get_gateway_school_by_id(db: Session, school_id: str) -> Optional[GatewaySchool]:
    return db.query(GatewaySchool).filter(GatewaySchool.school_id == school_id).first()

def get_gateway_schools(db: Session, skip: int = 0, limit: int = 100) -> List[GatewaySchool]:
    return db.query(GatewaySchool).offset(skip).limit(limit).all()

def create_or_update_gateway_school(db: Session, data: GatewaySchoolCreate) -> GatewaySchool:
    school = get_gateway_school_by_id(db, data.school_id)
    if school:
        for key, value in data.dict(exclude_unset=True).items():
            setattr(school, key, value)
    else:
        school = GatewaySchool(**data.dict())
        db.add(school)
    db.commit()
    db.refresh(school)
    return school
